<template>
  <view class="order-list">
    <!-- 顶部导航栏 -->
    <view class="nav-bar">
      <view class="nav-bar-left" @click="goBack">
        <image class="back-icon" src="/static/Images/shop/Fanhui.png" mode="aspectFit"></image>
      </view>
      <view class="nav-bar-title">{{ pageTitle }}</view>
      <view class="nav-bar-right"></view>
    </view>
    
    <!-- 订单列表 -->
    <view class="list-container">
      <block v-if="orderList.length > 0">
        <view 
          class="order-item" 
          v-for="(item, index) in orderList" 
          :key="index"
          @click="viewOrderDetail(item.id)"
        >
          <view class="order-header">
            <view class="order-type">
              <image class="type-icon" :src="getTypeIcon()" mode="aspectFit"></image>
              <text class="type-text">{{ getTypeText() }}</text>
            </view>
            <view class="order-status" :style="{color: getStatusColor(item.status)}">
              {{ getStatusText(item.status) }}
            </view>
          </view>
          
          <view class="order-content">
            <view class="order-info">
              <text class="order-title">{{ item.title }}</text>
              <text class="order-desc">{{ item.description }}</text>
              <view class="order-tags">
                <text class="tag" v-for="(tag, tagIndex) in item.tags" :key="tagIndex">{{ tag }}</text>
              </view>
            </view>
            <view class="order-price">
              <text class="price-value">¥{{ item.price }}</text>
              <text class="price-label">任务佣金</text>
            </view>
          </view>
          
          <view class="order-footer">
            <view class="order-time">{{ item.createTime }}</view>
            <view class="order-actions">
              <view class="action-btn" @click.stop="handleAction(item, 'cancel')" v-if="showCancelButton(item.status)">
                取消
              </view>
              <view class="action-btn primary" @click.stop="handleAction(item, 'accept')" v-if="showAcceptButton(item.status)">
                接单
              </view>
              <view class="action-btn primary" @click.stop="handleAction(item, 'complete')" v-if="showCompleteButton(item.status)">
                完成
              </view>
            </view>
          </view>
        </view>
      </block>
      
      <!-- 空状态 -->
      <view class="empty-state" v-if="orderList.length === 0">
        <!-- <image class="empty-icon" src="/static/Images/shop/.png" mode="aspectFit"></image> -->
        <text class="empty-text">暂无相关订单</text>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      type: '', // 订单类型：pending, processing, completed, guide, rights
      orderList: [
        {
          id: 1,
          title: '任务标题示例1',
          description: '这是一个任务描述示例，展示任务的基本信息和要求。',
          tags: ['标签1', '标签2'],
          price: '50.00',
          status: 'pending',
          createTime: '2023-06-15 14:30'
        },
        {
          id: 2,
          title: '任务标题示例2',
          description: '这是另一个任务描述示例，包含不同的任务内容和要求。',
          tags: ['标签3', '标签4'],
          price: '80.00',
          status: 'processing',
          createTime: '2023-06-14 09:15'
        },
        {
          id: 3,
          title: '任务标题示例3',
          description: '第三个任务描述示例，展示更多样化的任务类型。',
          tags: ['标签5', '紧急'],
          price: '120.00',
          status: 'completed',
          createTime: '2023-06-13 16:45'
        }
      ]
    }
  },
  computed: {
    pageTitle() {
      switch(this.type) {
        case 'pending':
          return '待办任务';
        case 'processing':
          return '进行中';
        case 'completed':
          return '已完成';
        case 'guide':
          return '攻略';
        case 'rights':
          return '权益';
        default:
          return '任务列表';
      }
    }
  },
  onLoad(options) {
    if (options.type) {
      this.type = options.type;
      this.filterOrders();
    }
  },
  methods: {
    // 返回上一页
    goBack() {
      uni.navigateBack();
    },
    
    // 根据类型筛选订单
    filterOrders() {
      // 实际应用中，这里应该调用API获取对应类型的订单
      if (this.type === 'pending') {
        this.orderList = this.orderList.filter(item => item.status === 'pending');
      } else if (this.type === 'processing') {
        this.orderList = this.orderList.filter(item => item.status === 'processing');
      } else if (this.type === 'completed') {
        this.orderList = this.orderList.filter(item => item.status === 'completed');
      } else if (this.type === 'guide' || this.type === 'rights') {
        // 攻略和权益页面可能有不同的数据结构
        this.orderList = [];
      }
    },
    
    // 获取类型图标
    getTypeIcon() {
      switch(this.type) {
        case 'pending':
          return '/static/Images/tesk/Yibang.png';
        case 'processing':
          return '/static/Images/tesk/Process.png';
        case 'completed':
          return '/static/Images/tesk/Chakancheng.png';
        case 'guide':
          return '/static/Images/tesk/Gonglue.png';
        case 'rights':
          return '/static/Images/tesk/Quanyi.png';
        default:
          return '/static/Images/tesk/Yibang.png';
      }
    },
    
    // 获取类型文本
    getTypeText() {
      switch(this.type) {
        case 'pending':
          return '待办任务';
        case 'processing':
          return '进行中';
        case 'completed':
          return '已完成';
        case 'guide':
          return '攻略';
        case 'rights':
          return '权益';
        default:
          return '任务';
      }
    },
    
    // 获取状态文本
    getStatusText(status) {
      switch(status) {
        case 'pending':
          return '待接单';
        case 'processing':
          return '进行中';
        case 'completed':
          return '已完成';
        default:
          return '未知状态';
      }
    },
    
    // 获取状态颜色
    getStatusColor(status) {
      switch(status) {
        case 'pending':
          return '#FF9500';
        case 'processing':
          return '#007AFF';
        case 'completed':
          return '#4CD964';
        default:
          return '#999999';
      }
    },
    
    // 查看订单详情
    viewOrderDetail(id) {
      uni.navigateTo({
        url: `/pages/tasks/detail?id=${id}&type=${this.type}`
      });
    },
    
    // 处理操作按钮点击
    handleAction(item, action) {
      switch(action) {
        case 'cancel':
          // 取消订单逻辑
          uni.showModal({
            title: '提示',
            content: '确定要取消此任务吗？',
            success: (res) => {
              if (res.confirm) {
                // 执行取消操作
                uni.showToast({
                  title: '已取消',
                  icon: 'success'
                });
              }
            }
          });
          break;
        case 'accept':
          // 接单逻辑
          uni.showModal({
            title: '提示',
            content: '确定要接此任务吗？',
            success: (res) => {
              if (res.confirm) {
                // 执行接单操作
                uni.showToast({
                  title: '接单成功',
                  icon: 'success'
                });
              }
            }
          });
          break;
        case 'complete':
          // 完成任务逻辑
          uni.showModal({
            title: '提示',
            content: '确认已完成此任务？',
            success: (res) => {
              if (res.confirm) {
                // 执行完成操作
                uni.showToast({
                  title: '已完成',
                  icon: 'success'
                });
              }
            }
          });
          break;
      }
    },
    
    // 是否显示取消按钮
    showCancelButton(status) {
      return status === 'pending' || status === 'processing';
    },
    
    // 是否显示接单按钮
    showAcceptButton(status) {
      return status === 'pending';
    },
    
    // 是否显示完成按钮
    showCompleteButton(status) {
      return status === 'processing';
    }
  }
}
</script>

<style scoped>
.order-list {
  min-height: 100vh;
  background-color: #f5f5f5;
}

/* 导航栏 */
.nav-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 90rpx;
  background-color: #FFFFFF;
  padding: 0 30rpx;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.nav-bar-left {
  width: 60rpx;
  height: 60rpx;
  display: flex;
  align-items: center;
  justify-content: flex-start;
}

.back-icon {
  width: 40rpx;
  height: 40rpx;
}

.nav-bar-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.nav-bar-right {
  width: 60rpx;
}

/* 列表容器 */
.list-container {
  padding: 20rpx;
}

/* 订单项 */
.order-item {
  background-color: #FFFFFF;
  border-radius: 20rpx;
  margin-bottom: 20rpx;
  padding: 30rpx;
  box-shadow: 0 4rpx 10rpx rgba(0, 0, 0, 0.05);
}

.order-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
  padding-bottom: 20rpx;
  border-bottom: 2rpx solid #F5F5F5;
}

.order-type {
  display: flex;
  align-items: center;
}

.type-icon {
  width: 40rpx;
  height: 40rpx;
  margin-right: 10rpx;
}

.type-text {
  font-size: 28rpx;
  color: #333;
  font-weight: bold;
}

.order-status {
  font-size: 26rpx;
  font-weight: bold;
}

.order-content {
  display: flex;
  margin-bottom: 20rpx;
}

.order-info {
  flex: 1;
}

.order-title {
  font-size: 30rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 10rpx;
  display: block;
}

.order-desc {
  font-size: 26rpx;
  color: #666;
  margin-bottom: 15rpx;
  display: block;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.order-tags {
  display: flex;
  flex-wrap: wrap;
}

.tag {
  font-size: 22rpx;
  color: #FF5151;
  background-color: rgba(255, 81, 81, 0.1);
  padding: 4rpx 16rpx;
  border-radius: 20rpx;
  margin-right: 10rpx;
  margin-bottom: 10rpx;
}

.order-price {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: center;
  margin-left: 20rpx;
}

.price-value {
  font-size: 36rpx;
  font-weight: bold;
  color: #FF5151;
}

.price-label {
  font-size: 22rpx;
  color: #999;
  margin-top: 5rpx;
}

.order-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 20rpx;
  border-top: 2rpx solid #F5F5F5;
}

.order-time {
  font-size: 24rpx;
  color: #999;
}

.order-actions {
  display: flex;
  gap: 20rpx;
}

.action-btn {
  font-size: 26rpx;
  color: #666;
  background-color: #F5F5F5;
  padding: 10rpx 30rpx;
  border-radius: 30rpx;
}

.action-btn.primary {
  color: #FFFFFF;
  background-color: #FF5151;
}

/* 空状态 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 100rpx 0;
}

.empty-icon {
  width: 200rpx;
  height: 200rpx;
  margin-bottom: 30rpx;
}

.empty-text {
  font-size: 28rpx;
  color: #999;
}
</style>